﻿using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IPAndCountry
{
    public class Rersult
    {
        public string ip, countryCode;
    }

    public static async CTask<Rersult> GetIPAndCountryCode()
    {
        HttpRes res = await HttpHelper.Get("https://ipinfo.io/json");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            return new Rersult() { ip = data["ip"].ToString(), countryCode = data["country"].ToString() };
        }
        res = await HttpHelper.Get("https://api.myip.com");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            return new Rersult() { ip = data["ip"].ToString(), countryCode = data["cc"].ToString() };
        }
        string ip = await GetIP();
        if (ip != null)
        {
            string countryCode = await GetCountryCodeByIP(ip);
            if (countryCode != null)
            {
                return new Rersult() { ip = ip, countryCode = countryCode };
            }
        }
        return null;
    }

    public static async CTask<string> GetIP()
    {
        HttpRes res = await HttpHelper.Get("https://api.ipify.org?format=json");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            if (data.Contains("ip"))
            {
                return data["ip"].ToString();
            }
        }
        res = await HttpHelper.Get("https://ipinfo.io/json");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            if (data.Contains("ip"))
            {
                return data["ip"].ToString();
            }
        }
        res = await HttpHelper.Get("https://ifconfig.me/ip");
        if (res.success)
        {
            return res.text;
        }
        res = await HttpHelper.Get("https://icanhazip.com");
        if (res.success)
        {
            return res.text;
        }
        res = await HttpHelper.Get("https://httpbin.org/ip");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            if (data.Contains("origin"))
            {
                return data["origin"].ToString();
            }
        }
        res = await HttpHelper.Get("https://api.myip.com");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            if (data.Contains("ip"))
            {
                return data["ip"].ToString();
            }
        }
        return null;
    }

    public static async CTask<string> GetCountryCodeByIP(string ip)
    {
        //第一家尝试
        HttpRes res = await HttpHelper.Get("https://api.ipdata.co/" + ip + "?api-key=45be73fa957e328ec5e9084d56fc3e198c21e4ae1954290b28fd4a56");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            if (data.Contains("country_code"))
            {
                return data["country_code"].ToString();
            }
        }
        //第二家备用再尝试
        res = await HttpHelper.Get("https://ipinfo.io/" + ip + "/json");
        if (res.success)
        {
            JsonData data = JsonMapper.ToObject(res.text);
            if (data.Contains("country"))
            {
                return data["country"].ToString();
            }
        }
        return null;
    }
}
